﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using SharpMap.Styles;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Collections.Specialized;

namespace SharpMap.Presentation.Wpf
{

    //just an interface for the factory. implemntation should be using reflection, etc...
    public class StyleViewModelFactory
    
    {
        readonly static HashSet<string> _genearlLayerProperties = new HashSet<string>
        {
            "MaxVisible","MinVisible","Enabled", "EnableOutline", "RenderingMode","TextRenderingHint"
        };

        private static readonly object _initSync = new object();
        static object _instance;
        static StyleViewModelFactory Instance
        {
            get
            {
                if (Thread.VolatileRead(ref _instance) == null)
                {
                    lock (_initSync)
                    {
                        if (Thread.VolatileRead(ref _instance) == null)
                        {
                            Thread.VolatileWrite(ref _instance, new StyleViewModelFactory());
                        }
                    }
                }
                return _instance as StyleViewModelFactory;
            }
        }

        public static ObservableCollection<INotifyPropertyChanged> GetStylePropertyList(IStyle style)
        {
            VectorStyle vs = style as VectorStyle;
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(style);
            ObservableCollection<INotifyPropertyChanged> retVal = new ObservableCollection<INotifyPropertyChanged>();
            foreach(PropertyDescriptor  propDesc in props)
            {
                string name = propDesc.Name;
                object value = propDesc.GetValue(style);
                string category = propDesc.Category;
                if (string.IsNullOrEmpty(category) || category.Equals("Misc"))
                {
                    category = GuessCategory(propDesc);
                }

                StyleViewEntry entry = new StyleViewEntry(name, value, category,style,propDesc);

                retVal.Add(entry); 
            }
            
            return retVal;
        }

        private static string GuessCategory(PropertyDescriptor propDesc)
        {
            string category = "Normal";
            string name = propDesc.Name;
            if (name.ToLower().Contains("select"))
            {
                category = "Selected";
            }
            else if (name.ToLower().Contains("highlight"))
            {
                category = "Highlighted";
            }
            else if (_genearlLayerProperties.Contains(name))
            {
                category = "General";
            }
            return category;
        }
    }

 

 
    


}
